const SegmentTree = require("../template")

function testSegmentTreeComplexScenarios() {
  console.log(
    "1. 复杂数据结构测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const st = new SegmentTree(arr)
      
      // 复杂数据结构操作
      const operations = [
        () => st.getRangeSum(0, 9), // 全部和
        () => st.getRangeSum(2, 7), // 部分和
        () => st.updateRangeValue(0, 4, 10), // 区间更新
        () => st.getRangeSum(0, 9), // 更新后查询
        () => st.updateSingleValue(5, 100), // 单点更新
        () => st.getSingleValue(5) // 单点查询
      ]
      
      const results = operations.map(op => op())
      
      return results[0] === 55 && // 1+2+...+10=55
             results[1] === 18 && // 3+4+5+6+7+8=33
             results[3] === 95 && // 更新后的和
             results[5] === 100 // 单点更新后的值
    })()
  )

  console.log(
    "2. 线段树动态负载测试:",
    (() => {
      const arr = Array.from({ length: 100 }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      // 动态负载测试
      const loadPatterns = [
        { operation: 'query', range: [0, 99] },
        { operation: 'update', range: [0, 49], val: 10 },
        { operation: 'query', range: [0, 99] },
        { operation: 'update', range: [50, 99], val: 20 },
        { operation: 'query', range: [0, 99] },
        { operation: 'point_update', idx: 25, val: 100 },
        { operation: 'point_query', idx: 25 }
      ]
      
      const results = []
      loadPatterns.forEach(pattern => {
        if (pattern.operation === 'query') {
          results.push(st.getRangeSum(pattern.range[0], pattern.range[1]))
        } else if (pattern.operation === 'update') {
          st.updateRangeValue(pattern.range[0], pattern.range[1], pattern.val)
        } else if (pattern.operation === 'point_update') {
          st.updateSingleValue(pattern.idx, pattern.val)
        } else if (pattern.operation === 'point_query') {
          results.push(st.getSingleValue(pattern.idx))
        }
      })
      
      return results.length >= 3 &&
             results[0] === 5050 && // 1+2+...+100=5050
             results[2] > results[0] && // 更新后和应该更大
             results[6] === 100 // 单点更新后的值
    })()
  )

  console.log(
    "3. 线段树边界条件测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const st = new SegmentTree(arr)
      
      // 边界条件测试
      const boundaryTests = [
        () => st.getRangeSum(0, 0), // 单点查询
        () => st.getRangeSum(4, 4), // 最后一个点
        () => st.getRangeSum(0, 4), // 全部范围
        () => st.updateSingleValue(0, 100), // 更新第一个点
        () => st.updateSingleValue(4, 200), // 更新最后一个点
        () => st.getSingleValue(0), // 查询第一个点
        () => st.getSingleValue(4) // 查询最后一个点
      ]
      
      const results = boundaryTests.map(test => test())
      
      return results[0] === 1 && // 第一个点
             results[1] === 5 && // 最后一个点
             results[2] === 15 && // 全部和
             results[5] === 100 && // 更新后的第一个点
             results[6] === 200 // 更新后的最后一个点
    })()
  )

  console.log(
    "4. 线段树错误恢复测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const st = new SegmentTree(arr)
      
      try {
        // 错误恢复测试
        const originalSum = st.getRangeSum(0, 4)
        
        // 执行一些操作
        st.updateRangeValue(0, 2, 10)
        st.updateSingleValue(3, 20)
        
        // 验证错误恢复
        const newSum = st.getRangeSum(0, 4)
        const pointValue = st.getSingleValue(3)
        
        return originalSum === 15 &&
               newSum > originalSum &&
               pointValue === 20
      } catch (error) {
        return false
      }
    })()
  )

  console.log(
    "5. 线段树内存管理测试:",
    (() => {
      const dataSize = 1000
      const arr = Array.from({ length: dataSize }, (_, i) => i + 1)
      
      // 内存管理测试
      const st = new SegmentTree(arr)
      
      // 执行大量操作
      for (let i = 0; i < 100; i++) {
        const l = Math.floor(Math.random() * dataSize)
        const r = l + Math.floor(Math.random() * (dataSize - l))
        st.updateRangeValue(l, r, i)
      }
      
      // 验证内存使用合理
      const finalSum = st.getRangeSum(0, dataSize - 1)
      
      return finalSum > 0 &&
             st !== null &&
             st !== undefined
    })()
  )

  console.log(
    "6. 线段树并发操作测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const st = new SegmentTree(arr)
      
      // 并发操作测试
      const operations = [
        () => st.getRangeSum(0, 4),
        () => st.updateRangeValue(0, 4, 10),
        () => st.getRangeSum(5, 9),
        () => st.updateSingleValue(5, 100),
        () => st.getRangeSum(0, 9),
        () => st.getSingleValue(5)
      ]
      
      const results = operations.map(op => op())
      
      return results[0] === 15 && // 前5个数的和
             results[2] === 40 && // 后5个数的和
             results[4] > 55 && // 更新后的总和
             results[5] === 100 // 单点更新后的值
    })()
  )

  console.log(
    "7. 线段树复杂查询测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const st = new SegmentTree(arr)
      
      // 复杂查询测试
      const queries = [
        { range: [0, 9], expected: 55 },
        { range: [0, 4], expected: 15 },
        { range: [5, 9], expected: 40 },
        { range: [2, 7], expected: 30 },
        { range: [1, 8], expected: 44 }
      ]
      
      const results = queries.map(query => {
        const result = st.getRangeSum(query.range[0], query.range[1])
        return result === query.expected
      })
      
      return results.every(result => result === true)
    })()
  )

  console.log(
    "8. 线段树状态一致性测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const st = new SegmentTree(arr)
      
      // 状态一致性测试
      const operations = [
        () => st.getRangeSum(0, 4),
        () => st.updateRangeValue(0, 2, 10),
        () => st.getRangeSum(0, 4),
        () => st.updateSingleValue(3, 20),
        () => st.getRangeSum(0, 4),
        () => st.getSingleValue(3)
      ]
      
      const results = operations.map(op => op())
      
      return results[0] === 15 && // 初始和
             results[2] > results[0] && // 更新后和更大
             results[4] > results[2] && // 再次更新后和更大
             results[5] === 20 // 单点更新后的值
    })()
  )

  console.log(
    "9. this上下文复杂场景测试:",
    (() => {
      const complexObj = {
        multiplier: 3,
        processComplex: function(arr) {
          const st = new SegmentTree(arr)
          const sum = st.getRangeSum(0, arr.length - 1)
          return sum * this.multiplier
        }
      }
      
      const arr = [1, 2, 3, 4, 5]
      return complexObj.processComplex(arr) === 45 // 15*3=45
    })()
  )

  console.log(
    "10. 复杂对象this绑定复杂场景测试:",
    (() => {
      const complexConfigObj = {
        config: { threshold: 10 },
        processWithConfig: function(arr) {
          const st = new SegmentTree(arr)
          const sum = st.getRangeSum(0, arr.length - 1)
          return sum >= this.config.threshold
        }
      }
      
      const arr = [1, 2, 3, 4, 5]
      return complexConfigObj.processWithConfig(arr) === true
    })()
  )

  console.log(
    "11. 线段树复杂操作序列测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const st = new SegmentTree(arr)
      
      // 复杂操作序列
      const operationSequence = [
        { op: 'query', range: [0, 9] },
        { op: 'update', range: [0, 4], val: 10 },
        { op: 'query', range: [0, 9] },
        { op: 'point_update', idx: 5, val: 100 },
        { op: 'point_query', idx: 5 },
        { op: 'query', range: [5, 9] },
        { op: 'update', range: [6, 9], val: 20 },
        { op: 'query', range: [0, 9] }
      ]
      
      const results = []
      operationSequence.forEach(seq => {
        if (seq.op === 'query') {
          results.push(st.getRangeSum(seq.range[0], seq.range[1]))
        } else if (seq.op === 'update') {
          st.updateRangeValue(seq.range[0], seq.range[1], seq.val)
        } else if (seq.op === 'point_update') {
          st.updateSingleValue(seq.idx, seq.val)
        } else if (seq.op === 'point_query') {
          results.push(st.getSingleValue(seq.idx))
        }
      })
      
      return results[0] === 55 && // 初始和
             results[2] > results[0] && // 更新后和更大
             results[4] === 100 && // 单点更新后的值
             results[5] > 40 && // 部分范围查询
             results[7] > results[2] // 最终和更大
    })()
  )

  console.log(
    "12. 线段树复杂场景综合测试:",
    (() => {
      const arr = Array.from({ length: 100 }, (_, i) => i + 1)
      const st = new SegmentTree(arr)
      
      // 综合复杂场景
      const scenarios = [
        // 场景1: 大量查询
        () => {
          let totalSum = 0
          for (let i = 0; i < 10; i++) {
            totalSum += st.getRangeSum(i * 10, (i + 1) * 10 - 1)
          }
          return totalSum === 5050 // 1+2+...+100=5050
        },
        // 场景2: 大量更新
        () => {
          for (let i = 0; i < 10; i++) {
            st.updateRangeValue(i * 10, (i + 1) * 10 - 1, i + 1)
          }
          return true
        },
        // 场景3: 更新后查询
        () => {
          const sum = st.getRangeSum(0, 99)
          return sum > 5050
        },
        // 场景4: 单点操作
        () => {
          st.updateSingleValue(50, 1000)
          return st.getSingleValue(50) === 1000
        },
        // 场景5: 最终验证
        () => {
          const finalSum = st.getRangeSum(0, 99)
          return finalSum > 5050
        }
      ]
      
      const results = scenarios.map(scenario => scenario())
      
      return results.every(result => result === true)
    })()
  )
}

testSegmentTreeComplexScenarios()
